package com.bestcem.xm.common.core.uitls;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author sh.pei <sh.pei@idiaoyan.com>
 * @version v1.0
 * @date 2021/7/8 17:15
 */
public class StringUtil {

    private static final char[] HEX_CHARS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    private StringUtil() {

    }

    /**
     * trim string, null或 trim后为空后返回null
     *
     * @param value 目标字符串
     * @return trim后结果
     */
    public static String trimToNull(String value) {
        String trim = null;
        if (value == null || (trim = value.trim()).length() == 0) {
            return null;
        }

        return trim;
    }

    /**
     * trim string, null或 trim后为空后返回 ""
     *
     * @param value 目标字符串
     * @return trim后结果
     */
    public static String trimToEmpty(String value) {
        value = StringUtil.trimToNull(value);
        return value == null ? "" : value;
    }

    /**
     * str trim后是否为空
     *
     * @param str 目标字符串
     * @return trim后是否为空
     */
    public static boolean isNullOrEmpty(String str) {
        str = StringUtil.trimToNull(str);
        return str == null;
    }

    public static String getId() {
        return new ObjectId().toHexString();
    }

    /**
     * 比较时不区分大小写
     *
     * @param str1
     * @param str2
     * @return
     */
    public static boolean equalsIgnoreCase(String str1, String str2) {
        return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2);
    }

    /**
     * 将字符串里所有的正则符号替换为空
     *
     * @param str 目标字符串
     * @return 替换后的字符串
     */
    public static String replaceAllRegexChar(String str) {
        return str.replaceAll("[\\$\\*\\.\\?\\+\\$\\^\\[\\]\\(\\)\\{\\}\\|\\\\\\/]", "");
    }

    public final static Set<Character> RE_KEYS =
            CollUtil.newHashSet('$', '(', ')', '*', '+', '.', '[', ']', '?', '\\', '^', '{', '}', '|', '/');

    /**
     * 将字符串里所有的正则符号替换为转义符
     *
     * @param str 目标字符串
     * @return 替换后的字符串
     */
    public static String escapeRegexChar(String str) {
        if (StrUtil.isBlank(str)) {
            return StrUtil.str(str);
        }

        final StringBuilder builder = new StringBuilder();
        int len = str.length();
        char current;
        for (int i = 0; i < len; i++) {
            current = str.charAt(i);
            if (RE_KEYS.contains(current)) {
                builder.append('\\');
            }
            builder.append(current);
        }
        return builder.toString();
    }

    public static List<String> convertLinkList(List<String> urlList) {
        List<String> result = new ArrayList<>();
        if (CollectionUtils.isEmpty(urlList)) {
            return result;
        }
        for (String url : urlList) {
            result.add(convertLink(url));
        }
        return result;
    }

    public static String convertLink(String url) {
        return "<a href=\"" + url + "\">" + url + "</a>";
    }

    public static String toHexString(byte[] var3) {
        if (var3 == null) {
            return null;
        }
        char[] chars = new char[24];
        int i = 0;
        int var4 = var3.length;

        for (int var5 = 0; var5 < var4; ++var5) {
            byte b = var3[var5];
            chars[i++] = HEX_CHARS[b >> 4 & 15];
            chars[i++] = HEX_CHARS[b & 15];
        }

        return new String(chars);
    }

    public static String valueOf(Object obj) {
        return (obj == null) ? "" : obj.toString();
    }

    /**
     * 预检查objectId是否是正确的
     *
     * @param objectId mongo中的id
     * @return true表示正常，false不是mongodb生成的id
     */
    public static boolean checkObjectId(String objectId) {
        //提前检查objectId是否正确
        try {
            new ObjectId(objectId);
        } catch (Exception exception) {
            return false;
        }
        return true;
    }

    /**
     * 字符串去空格
     *
     * @param str 字符串
     * @return 去空格字符串
     */
    public static String strTrim(String str) {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        return str.trim();
    }
}
